package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addToShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 2. 获取用户id
        Long userId = BaseContext.getCurrentId();
        // 1. 判断当前商品是套餐还是菜品
        if (shoppingCartDTO.getSetmealId() == null) {
            // 说明商品是菜品
            // 查询购物车项
            ShoppingCart shoppingCart = shoppingCartMapper.findByUserIdAndProductId(userId, null, shoppingCartDTO.getDishId());
            // 3. 设置number的值
            // 查询购物车表，根据用户id和商品id查询
            // 根据传入的id查询套餐或者菜品信息
            if (shoppingCart == null) {
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                // // 3.2 如果之前没添加过该商品，number = 1  新增
                shoppingCart = ShoppingCart.builder()
                        .name(dish.getName())
                        .image(dish.getImage())
                        .userId(userId)
                        .dishId(dish.getId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .number(1)
                        .amount(dish.getPrice())
                        .createTime(LocalDateTime.now())
                        .build();
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // // 3.1 如果之前添加过该商品，则在原有商品数量的基础上 +1   修改
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.update(shoppingCart);
            }
        } else {
            // 说明商品是套餐
            ShoppingCart shoppingCart = shoppingCartMapper.findByUserIdAndProductId(userId, shoppingCartDTO.getSetmealId(), null);
            // 3. 设置number的值
            // 查询购物车表，根据用户id和商品id查询
            // 根据传入的id查询套餐或者菜品信息
            if (shoppingCart == null) {
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                // // 3.2 如果之前没添加过该商品，number = 1  新增
                shoppingCart = ShoppingCart.builder()
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .userId(userId)
                        .setmealId(setmeal.getId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .number(1)
                        .amount(setmeal.getPrice())
                        .createTime(LocalDateTime.now())
                        .build();
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // // 3.1 如果之前添加过该商品，则在原有商品数量的基础上 +1   修改
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.update(shoppingCart);
            }
        }


    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    /**
     * 清空购物车商品
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
